<template>
  <el-dialog :title="formValidate.id ? $t('letterReviewMember.groupEdit') : $t('letterReviewMember.groupAdd')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('letterReviewMember.model.artificialReviewStage')" prop="stageId">
        <el-select v-model="formValidate.stageId" :placeholder="$t('letterReviewMember.prompt.artificialReviewStage')" style="width: 100%">
          <el-option
            v-for="item in allStage"
            :key="item.id"
            :label="item.flowName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.model.productType')" prop="productTypeList">
        <el-select v-model="formValidate.productTypeList" multiple :placeholder="$t('letterReviewMember.prompt.productType')" style="width: 100%">
          <el-option
            v-for="item in $t('letterReviewMember.productTypes')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.searchModel.reviewTeamName')" prop="groupName">
        <el-input v-model="formValidate.groupName" clearable :placeholder="$t('letterReviewMember.prompt.groupName')" :maxlength="100" />
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.model.introduction')">
        <el-input
          v-model="formValidate.groupDescription"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('letterReviewMember.prompt.introduction')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.searchModel.company')" prop="firmId">
        <el-select v-model="formValidate.firmId" clearable :placeholder="$t('letterReviewMember.prompt.status')" style="width: 100%">
          <el-option
            v-for="item in allFirm"
            :key="item.id"
            :label="item.firmName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('letterReviewMember.searchModel.status')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('letterReviewMember.prompt.status')" style="width: 100%">
          <el-option
            v-for="item in $t('letterReviewMember.status')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateCreditReviewGroup, addCreditReviewGroup } from '@/api/risk'
export default {
  name: 'GroupDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    allFirm: {
      type: Array,
      default: function() {
        return []
      }
    },
    allStage: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        productTypeList: null,
        groupName: null,
        groupDescription: null,
        firmId: null,
        stageId: null,
        status: null,
        releaseCase: null
      },
      rules: {
        productTypeList: [{ required: true, message: this.$t('letterReviewMember.model.productType') + this.$t('common.null'), trigger: 'blur' }],
        groupName: [{ required: true, message: this.$t('letterReviewMember.searchModel.reviewTeamName') + this.$t('common.null'), trigger: 'blur' }],
        firmId: [{ required: true, type: 'number', message: this.$t('letterReviewMember.searchModel.company') + this.$t('common.null'), trigger: 'blur' }],
        stageId: [{ required: true, type: 'number', message: this.$t('letterReviewMember.model.artificialReviewStage') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, type: 'number', message: this.$t('letterReviewMember.searchModel.status') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const formData = {
            id: this.formValidate.id,
            productTypeList: this.formValidate.productTypeList,
            groupName: this.formValidate.groupName,
            groupDescription: this.formValidate.groupDescription,
            firmId: this.formValidate.firmId,
            stageId: this.formValidate.stageId,
            status: this.formValidate.status,
            releaseCase: this.formValidate.status === 2 ? 2 : null
          }
          if (formData.id) {
            updateCreditReviewGroup(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addCreditReviewGroup(formData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
